package com.easybuilder.blog.dao;

import com.easybuilder.common.base.dao.BaseDao;
import com.easybuilder.blog.entity.Article;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import tk.mybatis.mapper.common.ExampleMapper;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface BlogArticleDao extends BaseDao<Article>, ExampleMapper<Article> {

    @Select("select count(1) from blog_article where 1 = 1 ${andSql}")
    int countByAndSql(@Param("andSql") String andSql);

    @Select("select * from blog_article where 1 = 1 ${andSql}")
    List<Article> selectByAndSql(@Param("andSql") String toString);

    /** 批量删除 */
    @Update("update blog_article set available = 0, update_user_id = #{userId}, update_nickname = #{nickname}, update_time = now() where id in (${ids})")
    int batchDelete(@Param("ids") String ids, @Param("userId") Long userId, @Param("nickname") String nickname);

    /** 统计文章数量 */
    @Select("select count(1) from blog_article union all select count(1) from blog_article where create_time like '${date}%'")
    List<Integer> countArticle(@Param("date") String date);

    /** 统计用户作品总量 */
    @Select("select count(1) from blog_article where create_user_id = #{userId} and available = 1")
    Long countArticleByUserId(@Param("userId") Long userId);
}